package cn.wolfcode.luowowo.comment.service;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private ITravelCommentRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public TravelComment save(TravelComment travelComment) {
        if (!StringUtils.hasLength(travelComment.getId())){
            travelComment.setCreateTime(new Date());
        }
        //处理评论的评论
        if (travelComment.getType()==TravelComment.TRAVLE_COMMENT_TYPE){
            if (StringUtils.hasLength(travelComment.getRefComment().getId()))
            travelComment.setRefComment(repository.findById(travelComment.getRefComment().getId()).get());
        }
        repository.save(travelComment);
        return travelComment;
    }

    @Override
    public Page query(TravelCommentQuery qo) {
        Query query = new Query();
        //设置条件
        if (qo.getTravelId()!=-1){
             query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        //totalPage
        long total = template.count(query, TravelComment.class);
        //pageSize
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(),
                Sort.Direction.DESC, "createTime");
        query.with(pageable);

        List<TravelComment> list = template.find(query, TravelComment.class);
        return new PageImpl(list,pageable,total);
    }
    //楼层
    @Override
    public Long getContent(Long travelId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("travelId").is(travelId));
        return template.count(query,TravelComment.class);
    }


    /* *//**
     * 点赞
     * @param toid
     * @param fromid
     *//*
    @Override
    public void commentThumbUp(String toid, Long fromid) {
        //判断当前用户是点赞还是取消点赞
        Optional<StrategyComment> ol = repository.findById(toid);
        StrategyComment comment = ol.get();
        if (comment==null){
            return;
        }
        List<Long> thumbuplist = comment.getThumbuplist();
        if (!thumbuplist.contains(fromid)){
            //点赞加1
            comment.setThumbupnum(comment.getThumbupnum()+1);
            thumbuplist.add(fromid);
        }else {
            //点赞减1
            comment.setThumbupnum(comment.getThumbupnum()-1);
            thumbuplist.remove(fromid);
        }
        repository.save(comment);

    }*/
}
